// pages/user_meet_info_mgr/user_meet_info_mgr.js
const db = wx.cloud.database()
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    meetInfoList: [],
    showList: [],
    inputValue: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  async onShow() {
    var that = this
    var meetInfoList = []
    await db.collection('order_info').where({
      _openid: app.globalData.openid
    }).get().then(res => {
      for (let i in res.data) {
        var meetInfo = {
          movieName: res.data[i].movieName,
          movieId: res.data[i].movieId,
          id: res.data[i]._id,
          name: res.data[i].lastname,
          phone: res.data[i].phone,
          orderDate: res.data[i].orderDate,
          price: '￥' + res.data[i].price,
          sessionId: res.data[i].orderSessionId
        }

        var strSeat = ''
        for (let j in res.data[i].seat) {
          strSeat = strSeat + (res.data[i].seat[j][0] + 1) + '排' + ( res.data[i].seat[j][1] + 1) + '座 '
        }
        meetInfo.seat = strSeat
        meetInfo.selectedSeat = res.data[i].seat
  
        meetInfoList.push(meetInfo)
      }
    })
    for (let idx in meetInfoList) {
      await db.collection('session_info').where({
        _id: meetInfoList[idx].sessionId
      }).get().then(res => {
        meetInfoList[idx].cinemaName = res.data[0].cinemaName
        meetInfoList[idx].room = res.data[0].room
        meetInfoList[idx].startTime = res.data[0].startTime
        meetInfoList[idx].date = res.data[0].date
        meetInfoList[idx].time = res.
        data[0].date + ' ' + res.data[0].startTime
      })
    }

    that.setData({
      meetInfoList: JSON.parse(JSON.stringify(meetInfoList)),
      showList: JSON.parse(JSON.stringify(meetInfoList))
    })
  },

  search(e) {
    var word = e.detail.value
    var showList = []
    for (const v of this.data.meetInfoList) {
      if (v.movieName.search(word) !== -1) {
        showList.push(JSON.parse(JSON.stringify(meetInfoList)))
      }
    }

    this.setData({showList: showList})
  },

  cancelSearch() {
    this.setData({
      inputValue: '',
      showList: JSON.parse(JSON.stringify(this.data.meetInfoList))
    })
  },

  formatDate(date) {
    var month = date.getMonth() + 1
    month = month.toString()
    if (date.getMonth() < 9) {
      month = '0' + month
    }
    
    var day = date.getDate()
    day = day.toString()
    if (date.getDate() < 10) {
      day = '0' + day
    }
    return `${date.getFullYear()}-${month}-${day}`;
  },

  async cancelMeetInfo(e) {
    var index = e.currentTarget.dataset.index
    var now = new Date()
    var todayDate = this.formatDate(now)
    if (todayDate === this.data.showList[index].date) {
      var splitRes = this.data.showList[index].startTime.split(':')
      var startTimeMinutesSum = parseInt(splitRes[0]) * 60 + parseInt(splitRes[1])
      var nowMinutesSum = now.getHours() * 60 + now.getMinutes()

      if (startTimeMinutesSum - nowMinutesSum <= 30) {
        wx.showToast({
          title: '电影开场前30分钟不可取消',
          icon: 'none',
          duration: 1000
        })

        return
      }
    }

    var flag = false
    await wx.showModal({
      title: '提示',
      content: '确认取消订单吗'
    }).then(res => {
      if (res.confirm) {
        flag = true
      }
    })

    if (!flag) {
      return
    }
    
    var that = this
    var showList = this.data.showList
    var meetInfoList = this.data.meetInfoList
    var selectedSeat = this.data.showList[index].selectedSeat
    var seat
    await db.collection('session_info').where({
      _id: that.data.showList[index].sessionId
    }).get().then(res => {
      seat = res.data[0].seat
      for (let i in selectedSeat) {
        var x = selectedSeat[i][0]
        var y = selectedSeat[i][1]
        seat[x][y] = 1
      }
    })

    await db.collection('session_info').doc(that.data.showList[index].sessionId).update({
      data: {
        seat: seat
      }
    })

    await db.collection('order_info').where({
      _id: showList[index].id
    }).remove().then(res => {
      var deleteIdx = 0
      while (deleteIdx < meetInfoList.length) {
        if (meetInfoList[deleteIdx].id === showList[index].id) {
          break
        }

        ++deleteIdx
      }
      showList.splice(index, 1)
      meetInfoList.splice(deleteIdx, 1)
      that.setData({
        showList: showList,
        meetInfoList: meetInfoList
      })
    })

    await wx.showToast({
      title: '订单已取消',
      icon: 'success',
      duration: 1000
    })
  }
})